{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "np.set_printoptions(suppress=True)  # 取消科学计数法输出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "radar_root = \"/mnt/g/WaterScenes/radar\" # @@适配点云数据路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "save_radar_map_root = \"/mnt/g/WaterScenes/radar_npz/VOCradar320\" # @@点云投影图保存路径，分辨率320*320\n",
    "os.makedirs(save_radar_map_root, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "54120"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "radar_files = os.listdir(radar_root)\n",
    "len(radar_files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# u and v represents the x and y on 2D image plane\n",
    "features_list = ['range', 'doppler', 'power', 'u', 'v'] # 距离、多普勒速度、能量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 54120/54120 [59:44<00:00, 15.10it/s]  \n"
     ]
    }
   ],
   "source": [
    "for i in tqdm(range(len(radar_files))):\n",
    "    example_file = os.path.join(radar_root, radar_files[i])\n",
    "    example_radar_points = pd.read_csv(example_file)[features_list].to_numpy()\n",
    "    example_radar_map = np.zeros((len(features_list)-2, 320, 320))\n",
    "    for channel in range(len(features_list)-2):\n",
    "        for line in example_radar_points:\n",
    "            try:\n",
    "                # 原脚本是将点云都压缩到320，但是对应图片等比例缩放并灰色填充，导致点云拉伸\n",
    "                # row_index = int(line[-2]/6) # 映射到宽，1~320\n",
    "                # column_index = int(line[-1]/3.375) # 映射到高，1~320\n",
    "                row_index = int(line[-2]/6) # 映射到宽，1~320\n",
    "                column_index = int(line[-1]/6+70) # 映射到高，70~250，这保证点云缩放比例不变，且与Achelous的图片输入定义一致\n",
    "                if example_radar_map[channel][row_index][column_index] != 0 and row_index>=1:\n",
    "                    row_index -= 1\n",
    "\n",
    "                example_radar_map[channel][row_index][column_index] = line[channel]\n",
    "            except:\n",
    "                continue\n",
    "\n",
    "    example_radar_map = example_radar_map.transpose(0, 2, 1)\n",
    "    np.savez_compressed(os.path.join(save_radar_map_root, radar_files[i][:-4]+'.npz'), example_radar_map)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
